//
//  BRMInfoTImeView.m
//  BrezzeSaaS
//
//  Created by macos on 16.3.22.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRMInfoTImeView.h"
#import "BRMSwitchUIView.h"
#import "BRMTimePickView.h"
#import "BRDatePickerView.h"

@interface BRMInfoTImeView ()
{
    UIView *_headerView;
    UIView *_contentView;
    
    BRMSwitchUIView *_switchBtn;
}

@property MASConstraint * headViewBottomContact0;
@property MASConstraint * headViewBottomContact1;

@property (nonatomic,strong) NSArray *weeks;

@end

@implementation BRMInfoTImeView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - func --
- (void)setDto:(BRMTimeDicJsonModel *)dto {
    _dto = dto;
    self.isOn = dto.check;
    _switchBtn.isOn = dto.check;
    NSArray *arr = dto.businessHours;
    for (int i = 0; i < arr.count; i++) {
        
        BRMTimeJsonModel *model = arr[i];
        BRMInfoDayView *dayView = [_contentView viewWithTag:1000 + i];
        dayView.isOn = model.check;
        dayView.isHoursOn = model.allDay;
        NSMutableArray *times = [NSMutableArray array]; // 用显示数量
        for (int j = 1; j <= 4; j++) {
            NSString *key = [NSString stringWithFormat:@"rangeTime%d",j];
            NSArray *arr = [model valueForKey:key];
            if (j == 1 && arr.count == 0) {
                // 没有就保底设置一个时间
                arr = @[@"09:00",@"20:00"];
                [model setValue:arr forKey:key];
            }
            [dayView addTime:arr by:j - 1];
            if (arr.count != 0) {
                [times addObject:arr];
            }
        }
        dayView.count = times.count;
        [dayView lastAdd:times.count];
    }
    
}

- (void)toEdit:(QMUIButton *)btn {
    
    if (btn.isSelected) {
        // to save
        btn.selected = NO;
        self.isOn = btn.isSelected;
        
        [self saveTime];
        
        return;
    }
    btn.selected = YES;
    self.isOn = btn.isSelected;
//    if (self.toEditBlock) {
//        self.toEditBlock();
//    }
}

- (void)toSwitchOn:(BOOL)isON {
    
    if (self.switchOnBlock) {
        self.switchOnBlock(isON);
    }
//    CGFloat time = isON ? 0 : 0.2;
//    [self jk_performAfter:time block:^{
//            self.isOn = isON;
//    }];
    
}

- (void)checkIsOn:(BOOL)isOn {
    self.isOn = isOn;
    self.dto.check = isOn;
    [self saveTime];
}

- (void)hiddenContentV:(BOOL)isHidd {
    _contentView.hidden = isHidd;
}

- (void)saveTime {
    
    if (self.dto) {
//        NSMutableArray *arr = [NSMutableArray array];
//        for (BRMTimeJsonModel *model in self.dtos) {
//
//            [arr addObject:[model mj_keyValues]];
//        }
        NSDictionary *dic = [self.dto mj_keyValues];
        NSString *jsonStr = [dic mj_JSONString];
        if (self.saveBlock) {
            self.saveBlock(@{@"text":jsonStr,@"title":@"openTime"});
        }
    }
}

- (void)setIsOn:(BOOL)isOn {
    _isOn = isOn;
    
    if (isOn) {
        [self.headViewBottomContact1 install];
        [self.headViewBottomContact0 uninstall];
    }else {
        [self.headViewBottomContact0 install];
        [self.headViewBottomContact1 uninstall];
        
    }
    [self hiddenContentV:!isOn];
}

- (void)switchON:(BOOL)isOn by:(NSInteger)tag type:(NSInteger)type {
    
    BRMTimeJsonModel *model = self.dto.businessHours[tag];
    if (type == 0) {
        model.check = isOn;
    }else {
        model.allDay = isOn;
    }
    [self saveTime];
}

- (void)deleteDayTime:(NSInteger)index by:(NSInteger)tag {
    
    BRMTimeJsonModel *model = self.dto.businessHours[tag];
    NSString *key = [NSString stringWithFormat:@"rangeTime%ld",index+1];
    [model setValue:@[] forKey:key];
    
//    NSLog(@"model - %@ \n dots - %@",[model mj_keyValues],[self.dtos[tag] mj_keyValues]);
//    NSMutableArray *mutArr = [NSMutableArray array];
    [self saveTime];
}

- (void)addDayTime:(NSArray *)times index:(NSInteger)index by:(NSInteger)tag {
    
    BRMTimeJsonModel *model = self.dto.businessHours[tag];
    NSString *key = [NSString stringWithFormat:@"rangeTime%ld",index+1];
    [model setValue:times forKey:key];

    NSLog(@"model - %@ \n dots - %@",[model mj_keyValues],[self.dto.businessHours[tag] mj_keyValues]);
//    NSMutableArray *mutArr = [NSMutableArray array];
    BRMInfoDayView *dayView = [_contentView viewWithTag:1000 + tag];
    [dayView addTime:times by:index];
    
    [self saveTime];
}



- (void)showTimePick:(NSInteger)index by:(NSInteger)tag {
    
    BRMTimePickView *pick = [[BRMTimePickView alloc] init];
    [DefaultTipsParentView addSubview:pick];
    [pick mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [pick br_show];
    WS(weakSelf);
    pick.timeDone = ^(NSArray * _Nonnull times) {
        
        [weakSelf addDayTime:times index:index by:tag];
    };
    
}

#pragma mark - UI --
- (void)setUI {
    
    self.layer.cornerRadius = 13;
    self.layer.masksToBounds = YES;
    self.backgroundColor = UIColorWhite;
    
    UIView *head = [[UIView alloc] init];
    [self addSubview:head];
    UIView *contentView = [[UIView alloc] init];
    [self addSubview:contentView];
    _headerView = head;
    _contentView = contentView;
    [head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.mas_equalTo(44);
    }];
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
    }];
    
    [head mas_updateConstraints:^(MASConstraintMaker *make) {
        _headViewBottomContact0 = make.bottom.offset(0);
        _headViewBottomContact1 = make.bottom.equalTo(_contentView.mas_top).offset(0);
    }];
    
    self.isOn = NO;
    
    UIView *line = [BRToolsView getLineView];
    [contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.offset(0);
        make.right.offset(0);
        make.top.offset(0);
    }];
    
    
    BRMSwitchUIView *switchBtn = [[BRMSwitchUIView alloc] init];
    [head addSubview:switchBtn];
    [switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-16);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(24);
    }];
    _switchBtn = switchBtn;
    
    QMUILabel *titleL = [BRToolsView br_label:Localized(@"home_businessHours") textColor:kColorTitle font:[UIFont fontM:16]];
    [head addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.equalTo(switchBtn.mas_centerY).offset(0);
    }];
    
    
    NSArray *weeks = self.weeks;
    BRMInfoDayView *firstView;
    for (int i = 0; i < weeks.count; i++) {
        BRMInfoDayView *dayView = [[BRMInfoDayView alloc] init];
        dayView.titlsStr = weeks[i];
        dayView.tag = 1000 + i;
        [contentView addSubview:dayView];
        [dayView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.top.offset(0);
            }else {
                make.top.equalTo(firstView.mas_bottom).offset(0);
            }
            if (i == 6) {
                make.bottom.offset(0);
            }
            make.left.right.offset(0);
        }];
        dayView.lineHidde = i == weeks.count - 1;
        firstView = dayView;
        WS(weakSelf);
        dayView.timeAddBlock = ^(NSInteger index) {
            [weakSelf showTimePick:index by:i];
        };
        
        dayView.deleteTimeBlock = ^(NSInteger index) {
            [weakSelf deleteDayTime:index by:i];
        };
        dayView.switchBlock = ^(BOOL isON, NSInteger type) {
            [weakSelf switchON:isON by:i type:type];
        };
    }
    
    WS(weakSelf);
    switchBtn.switchClickBlock = ^(BOOL isOn) {
        [weakSelf toSwitchOn:isOn];
    };
}

- (NSArray *)weeks {
    
    if (!_weeks) {
        _weeks = @[Localized(@"shop_monday"),Localized(@"shop_tuesday"),Localized(@"shop_wednesday"),Localized(@"shop_thursday"),Localized(@"shop_friday"),Localized(@"shop_saturday"),Localized(@"shop_sunday")];
    }
    
    return _weeks;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
